import org.apache.commons.io.IOUtils;

import java.net.URLEncoder;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.HttpURLConnection;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;

/**
 * Date: Feb 27, 2007 Time: 7:49:39 PM
 *
 * @author pat
 */
public class Test
{
    private static String encode( String name, String value ) {
        return URLEncoder.encode( name ) + "=" + URLEncoder.encode( value );
    }

    private static String getAuth( String s ) {
        int i = s.indexOf("Auth");
        return s.substring(i+5, s.length()-1); // Auth=
    }

        //String url = "http://localhost:8080/servlets-examples/servlet/RequestHeaderExample";
        //String authHeader = "AuthSub token=\""+auth+"\"";

    public static void main( String[] args ) throws IOException
    {
        String cellURL = "http://spreadsheets.google.com/feeds/cells/o10922066485030349530.6403441927775707849/od6/private/full";
        System.out.println(
            "cellFeedURLtoListFeedURL( cellURL ) = " + cellFeedURLtoListFeedURL( cellURL ) );

        //String auth = getAuth();
        //System.out.println( "auth = " + auth );
        //listSpreadhseets( auth );
    }
    /**
     * We infer the list URL from the cell URL...
     * This is not hard to do, but I'm not sure if it's future proof.
     */
    public static String cellFeedURLtoListFeedURL( String cellURL ) {
        //           http://spreadsheets.google.com/feeds/list/key/worksheetId/private/full
        // postURL = http://spreadsheets.google.com/feeds/list/o10922066485030349530.6403441927775707849/od6/private/full
        // cellURL = http://spreadsheets.google.com/feeds/cells/o10922066485030349530.6403441927775707849/od6/private/full
        int i = cellURL.indexOf("feeds/cells");
        return cellURL.substring(0,i) + "feeds/list" + cellURL.substring(i+11);
    }

    /*
    private static void listSpreadhseets( String auth ) throws IOException
    {
        String urlt = "http://spreadsheets.google.com/feeds/spreadsheets/private/full";
        URL url = new URL( urlt );
        HttpURLConnection urlcon =
            (HttpURLConnection)url.openConnection();

        //String authHeader = "AuthSub token=&quot;";
        String authHeader = "GoogleLogin auth="+auth;
        //authHeader += URLEncoder.encode(auth);
        //authHeader += "&quot;";

        System.out.println( "authHeader = " + authHeader );
        //urlcon.setRequestProperty( "Content-type",
        //    "application/x-www-form-urlencoded" );
        urlcon.setRequestProperty( "Authorization", authHeader );
        urlcon.setRequestMethod( "GET" );
        urlcon.setDoInput( true );
        int responseCode = urlcon.getResponseCode();
        System.out.println( "responseCode = " + responseCode );
        InputStream ins;
        if ( responseCode == 200 )
            ins = urlcon.getInputStream();
        else
            ins = urlcon.getErrorStream();
        String response = IOUtils.toString( ins );
        System.out.println( "response = " + response );
    }
    */

    /*
    public static String getAuth() throws IOException
    {
        StringBuffer sb = new StringBuffer();
        sb.append( encode( "accountType", "GOOGLE" ) );
        sb.append("&");
        sb.append( encode("Email", "patniemeyer@gmail.com") );
        sb.append("&");
        sb.append( encode( "Passwd", "patsafe1") );
        sb.append("&");
        sb.append( encode( "source", "pat-testapp-1") );
        sb.append("&");
        sb.append( encode( "service", "wise") );
        String formData = sb.toString();
        System.out.println( "formData = " + formData );

        String urlt = "https://www.google.com/accounts/ClientLogin";
        URL url = new URL( urlt );
        HttpURLConnection urlcon =
            (HttpURLConnection)url.openConnection();
        urlcon.setRequestMethod( "POST" );
        urlcon.setRequestProperty( "Content-type",
            "application/x-www-form-urlencoded" );
        urlcon.setDoOutput( true );
        urlcon.setDoInput( true );
        PrintWriter pout = new PrintWriter( new OutputStreamWriter(
            urlcon.getOutputStream(), "8859_1" ), true );
        pout.print( formData );
        pout.flush();

        int responseCode = urlcon.getResponseCode();
        Map<String, List<String>> map = urlcon.getHeaderFields();
        Set keys = map.keySet();
        for ( Object key : keys )
        {
            List<String> values = map.get( key );
            System.out.println( "key =" + key + " values = " + values );
        }
        System.out.println( "responseCode = " + responseCode );

        InputStream ins = urlcon.getInputStream();
        String response = IOUtils.toString( ins );
        System.out.println( "response = " + response );

        return getAuth( response.toString() );
    }
    */
}
